package com.hexin.actionbar;


import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 生成一个SpinnerAdapter
		SpinnerAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.student, android.R.layout.simple_spinner_dropdown_item);
		// 得到ActionBar
		ActionBar actionBar = getActionBar();
		// 将ActionBar的操作模型设置为NAVIGATION_MODE_LIST
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		// 为ActionBar设置下拉菜单和监听器
		actionBar.setListNavigationCallbacks(adapter, new DropDownListenser());
		// ActionBar.NAVIGATION_MODE_TABS模式
		// actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		// TabListener tl = new ActionBar.TabListener() {
		// @Override
		// public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// }
		// @Override
		// public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// }
		// @Override
		// public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// }
		// };
		// Tab tab1 = actionBar.newTab();
		// tab1.setText("Tab1");
		// tab1.setTabListener(tl);
		// actionBar.addTab(tab1);
	}

	@Override
	protected void onStart() {
		super.onStart();
		ActionBar actionBar = this.getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		int flags = ActionBar.DISPLAY_SHOW_TITLE;
		int change = actionBar.getDisplayOptions() ^ flags;
		actionBar.setDisplayOptions(change, flags);

		flags = ActionBar.DISPLAY_USE_LOGO;
		change = actionBar.getDisplayOptions() ^ flags;
		actionBar.setDisplayOptions(change, flags);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.actions, menu);
		
		menu.add("Normal item");
		MenuItem actionItem = menu.add("Save");
		actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		actionItem.setIcon(android.R.drawable.ic_menu_save);
		
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, SecondTestActivity.class);
			startActivity(intent);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/** * 实现 ActionBar.OnNavigationListener接口 */
	class DropDownListenser implements OnNavigationListener {
		// 得到和SpinnerAdapter里一致的字符数组
		String[] listNames = getResources().getStringArray(R.array.student);

		/* 当选择下拉菜单项的时候，将Activity中的内容置换为对应的Fragment */
		public boolean onNavigationItemSelected(int itemPosition, long itemId) { // 生成自定的Fragment
			StudentInfo student = new StudentInfo();
			FragmentManager manager = getFragmentManager();
			FragmentTransaction transaction = manager.beginTransaction();
			// 将Activity中的内容替换成对应选择的Fragment
			transaction.replace(R.id.context, student, listNames[itemPosition]);
			transaction.commit();
			return true;
		}
	}

}